using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using TemplateWeb.DAO;
using TemplateWeb.Helpers;

namespace TemplateWeb.DTO
{
	/// <summary>
	/// Data transfer object which is associated with DanhMucChinh table in database
	/// </summary>
	public class DanhMucChinhDTO
	{
		public int ID { get; set; }
		public string TenDanhMucChinh { get; set; }
        public List<DanhMucConDTO> DanhMucCon { get; set; }

		public static DanhMucChinhDTO Translate(DataRow row)
		{
			var dto = new DanhMucChinhDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				TenDanhMucChinh = row["TenDanhMucChinh"].ToString(),
			};

            dto.DanhMucCon = new List<DanhMucConDTO>();

            // Select danh muc con
            var rpt = new DanhMucConDAO(WebConfiguration.DbConnectionString);
            var objDS = rpt.SelectByIDDanhMucChinh(dto.ID);
            if (objDS != null && objDS.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in objDS.Tables[0].Rows)
                {
                    dto.DanhMucCon.Add(DanhMucConDTO.Translate(datarow));
                }
            }
			return dto;
		}
	}
}